package com.xique.park.controller.client.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xique.client.api.domain.ClientUserInfo;
import com.xique.common.core.constant.UserConstants;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.bean.domain.ParkCarMngtype;
import com.xique.park.bean.domain.ParkInfo;
import com.xique.park.bean.vo.client.ClientCommonVo;
import com.xique.park.controller.client.web.bean.domain.*;
import com.xique.park.controller.client.web.mapper.SystemUserRoleMapper;
import com.xique.park.controller.client.web.service.service.*;
import com.xique.park.service.service.IParkCarMngtypeService;
import com.xique.park.service.service.IParkInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2021/7/28 17:23
 */
@RestController
@RequestMapping("/common")
public class ClientCommonController {

    @Autowired
    private IParkCarMngtypeService parkCarMngtypeService;

    /**
     * 车辆管理类型列表
     * @author shd
     * @date 2021/7/29 10:35
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("mngtype")
    @ApiOperation("车辆管理类型列表")
    public AjaxResult mngtype() {
        ParkCarMngtype parkCarMngtype = new ParkCarMngtype();
        parkCarMngtype.setParkId(SecurityUtils.getParkId());
        List<ParkCarMngtype> list = parkCarMngtypeService.selectParkCarMngtypeList(parkCarMngtype);
        List<ClientCommonVo> vos = OrikaUtil.converts(list, ClientCommonVo.class);
        return AjaxResult.success(vos);
    }

    @Autowired
    private IClientUserInfoService clientUserInfoService;

    @Autowired
    private IParkInfoService parkInfoService;

    @Autowired
    private IClientUserService clientUserService;

    @Autowired
    private IClientLoginInfoService clientLoginInfoService;

    @Autowired
    private IClientService clientService;

    @Autowired
    private IOpengateRecordsService opengateRecordsService;

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/changeData")
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AjaxResult changeData() {

        QueryWrapper<ClientUserInform> condition = new QueryWrapper<>();
        condition.eq("del_flag", "0");
        List<ClientUserInform> clientUserList = clientUserInfoService.list(condition);

        List<Long> parkIds = clientUserList.stream().map(ClientUserInform::getParkId).distinct().collect(Collectors.toList());
        List<ParkInfo> parkInfoList = parkInfoService.listByIds(parkIds);
        Map<Long, Long> parkInfoMap = parkInfoList.stream().collect(Collectors.toMap(ParkInfo::getId, ParkInfo::getDeptId));

        QueryWrapper<SysUser> userCondition = new QueryWrapper<>();
        userCondition.eq("del_flag", 0);
        List<SysUser> list = sysUserService.list(userCondition);
        List<String> loginNames = list.stream().map(SysUser::getLoginName).collect(Collectors.toList());

        List<SysUser> userList = new ArrayList<>(clientUserList.size());
        clientUserList.forEach(item -> {
            if (!loginNames.contains(item.getLoginName())) {
                SysUser user = new SysUser();
                user.setParkId(item.getParkId());
                user.setLoginName(item.getLoginName());
                user.setUserName(item.getUserName());
                user.setPhonenumber(item.getPhone());
                user.setSex(item.getSex());
                user.setPassword(item.getPassword());
                user.setOnlineStatus(item.getOnlineStatus());
                user.setStatus("normal".equals(item.getStatus()) ? "0" : "1");
                user.setUserType("02");
                user.setType(UserConstants.Type.CLIENT);
                user.setClientUserId(item.getId());
                if (StringUtils.isNotNull(parkInfoMap.get(item.getParkId()))) {
                    user.setDeptId(parkInfoMap.get(item.getParkId()));
                }

                userList.add(user);
            }
        });
        // TODO sysUser insert
        sysUserService.saveBatch(userList);

        List<ClientUser> clientUserList1 = new ArrayList<>();
        List<ClientLoginInfo> loginInfoList = new ArrayList<>();
        List<Client> clientList = new ArrayList<>();
        List<OpengateRecords> recordsList = new ArrayList<>();
        userList.forEach(item -> {
            ClientUser clientUser = new ClientUser();
            clientUser.setUserId(item.getClientUserId());
            clientUser.setSysUserId(item.getUserId());

            clientUserList1.add(clientUser);

            ClientLoginInfo loginInfo = new ClientLoginInfo();
            loginInfo.setUserId(item.getClientUserId());
            loginInfo.setSysUserId(item.getUserId());

            loginInfoList.add(loginInfo);

            Client client = new Client();
            client.setLoginUserId(item.getClientUserId());
            client.setSysUserId(item.getUserId());

            clientList.add(client);

            OpengateRecords records = new OpengateRecords();
            records.setOprClientUser(item.getClientUserId());
            records.setOprUser(item.getUserId());

            recordsList.add(records);
        });
        // TODO clientUser clientLoginInfo client opengateRecords update
        clientUserService.updateSysUserId(clientUserList1);

        clientLoginInfoService.updateSysUserId(loginInfoList);

        clientService.updateSysUserId(clientList);

        opengateRecordsService.updateSysUserId(recordsList);

        return null;
    }
}
